public class IntegerReplacement {

    public static void main(String[] args) {
        IntegerReplacement integerReplacement = new IntegerReplacement();
        System.out.println(integerReplacement.integerReplacement(2147483647));
    }

    public int integerReplacement(int n) {
        return process(n);
    }

    public int process(long n) {
        if (n == 1) return 0;
        if (n % 2 == 0) {
            return process(n / 2) + 1;
        } else {
            return Math.min(process(n + 1), process(n - 1)) + 1;
        }
    }
}
